//
//  HJOriginalStatusView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/20.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit
import SnapKit

class HJOriginalStatusView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //view底部约束
    var bottomCons:Constraint?
    var statueViewModel:HJStatusViewModel?{
        didSet{
            headImageView.sd_setImageWithURL(NSURL(string: statueViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            nameLabel.text = statueViewModel?.status?.user?.name
            memberImgView.image = statueViewModel?.memberImage
            timeLabel.text = statueViewModel?.creatAtText
            
            source.text = statueViewModel?.sourceText
            contentLabel.text = statueViewModel?.status?.text
            bottomCons?.uninstall()
            
            if let pic_urls = statueViewModel?.status?.pic_urls where pic_urls.count > 0{
                
                pictureView.dataInfo = (pic_urls,statueViewModel!.originalPictureViewSize)
                
                pictureView.pic_urls = pic_urls
                pictureView.hidden = false
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentLabel).offset(HJStatusCellMargin).constraint
                })
            }
            
        }
    }
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImgView)
        addSubview(timeLabel)
        addSubview(source)
        addSubview(contentLabel)
        addSubview(verifiedImageView)
        addSubview(pictureView)
        
        //添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(HJStatusCellMargin)
            make.width.height.equalTo(HJStatusCellHeadImgHW)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HJStatusCellMargin)
        }
        memberImgView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HJStatusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HJStatusCellMargin)
        }
        source.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HJStatusCellMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HJStatusCellMargin)
            make.leading.equalTo(headImageView)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HJStatusCellMargin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) -> Void in
            bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(HJStatusCellMargin).constraint
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing).offset(-2)
            make.centerY.equalTo(headImageView.snp_bottom).offset(-2)
        }
    }
    
    //懒加载控件
    //头像
    private lazy var headImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_small"))
        
        return imageView
    
    }()
    
    //昵称
    private lazy var nameLabel:UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: HJStatusCellNameFont)
        label.text = "韩大大"
        return label
    }()
    //会员图片
    private lazy var memberImgView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel:UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: HJStatusCellTimeFont)
        label.text = "2016:4:20"
        return label
    }()
    //来源
    private lazy var source:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HJStatusCellTimeFont)
        label.text = "iPhone7lpus"
        return label
    }()
    //微博正文
    private lazy var contentLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HJStatusCellContentFont, preferredMaxLayoutWidth: ScreenWidth - 2 * HJStatusCellMargin)
        return label
    }()
    //大V认证
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //配图视图
    private lazy var pictureView:HJStatusPictureView = HJStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
}










